package com.example.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.auth.domain.UserIdcard;

import com.example.auth.mapper.UserIdcardMapper;
import com.example.auth.service.UserIdcardService;
import com.example.config.exception.GlobalExcetion;
import com.example.util.sql.DBSql;
import com.example.util.sql.IdUtils;
import com.example.util.sql.SqlGenerator;
import com.example.util.sql.SqlResult;
import com.example.util.tableshard.TableShardingAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.Date;

/**
* @author CETC
* @description 针对表【user_idcard_0】的数据库操作Service实现
* @createDate 2025-11-01 16:17:22
*/
@Service
public class UserIdcardServiceImpl extends ServiceImpl<UserIdcardMapper, UserIdcard>
    implements UserIdcardService {

    /**
     * 表名前缀
     */
    private final static String USER_IDCARD_TABLE_SUFFIX = "user_idcard_";

    /**
     * 用户表数量
     */
    private final static Integer USER_IDCARD_TABLE_COUNT=3;

    private final static String USER_IDCARD_SUFFIX="idcard_";

    @Autowired
    private DataSource dataSource;

    private String getTableIndexByIDcard(String idCard) {
        if(idCard==null || idCard.isEmpty()){
            return "";
        }
        Integer tableIndex= TableShardingAlgorithm.getTableIndexByConsistentHash(idCard,USER_IDCARD_TABLE_COUNT);
        return tableIndex.toString();
    }


    private String getTableByIDcard(String idCard) {
        String index=getTableIndexByIDcard(idCard);
        return USER_IDCARD_TABLE_SUFFIX+index;
    }


    private String createUUIdByIDcard(String idCard) {
        String id= IdUtils.getUUID();
        String index=getTableByIDcard(idCard);
        return USER_IDCARD_SUFFIX+index+"_"+id;
    }

    private String getTableByUUID(String uuid) {
        String[] strings=uuid.split("_");
        if(strings.length < 2){
            return "";
        }
        return USER_IDCARD_TABLE_SUFFIX+strings[1];
    }

    @Override
    public UserIdcard saveUserIdcard(UserIdcard userIdcard) throws GlobalExcetion {
        if(userIdcard.getUserIdcard()==null || userIdcard.getUserId()==null){
            throw new GlobalExcetion(1,"用户id和idcard不得为空");
        }
        String tableName=getTableByIDcard(userIdcard.getUserIdcard());
        String uuid=createUUIdByIDcard(userIdcard.getUserIdcard());
        userIdcard.setUuid(uuid);
        userIdcard.setUpdateTime(new Date());
        userIdcard.setCreateTime(new Date());
        SqlResult sqlResult= SqlGenerator.generateInsert(tableName,userIdcard);
        DBSql dbSql=new DBSql(dataSource);
        dbSql.executeInsert(sqlResult.getSql(),sqlResult.getParams().toArray());
        return userIdcard;
    }

    @Override
    public UserIdcard getByIdCard(String idCard) throws GlobalExcetion {
        String tableName=getTableByIDcard(idCard);
        return this.baseMapper.getByIdcard(tableName,idCard);
    }

    @Override
    public int deleteByIdCard(String idCard) throws GlobalExcetion {
        String tableName=getTableByIDcard(idCard);
        return this.baseMapper.deleteByIdcard(tableName,idCard);
    }

    @Override
    public UserIdcard updateUserIdcard(UserIdcard userIdcard) throws GlobalExcetion {
        if(userIdcard.getUserIdcard()==null || userIdcard.getUuid()==null){
            throw new GlobalExcetion(1,"重要数据不得为空");
        }
        String tableName=getTableByIDcard(userIdcard.getUserIdcard());
        DBSql dbSql=new DBSql(dataSource);
        userIdcard.setUpdateTime(new Date());
        SqlResult sqlResult=SqlGenerator.generateUpdateById(tableName,userIdcard,"uuid");
        dbSql.executeUpdate(sqlResult.getSql(),sqlResult.getParams().toArray());
        return userIdcard;
    }
}




